/**
 * https://leetcode.com/problems/word-pattern/?envType=study-plan-v2&envId=top-interview-150
 *
 * Given a pattern and a string s, find if s follows the same pattern.
 *
 * Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s.
 *
 * Example 1:
 * Input: pattern = "abba", s = "dog cat cat dog"
 * Output: true
 *
 * Example 2:
 * Input: pattern = "abba", s = "dog cat cat fish"
 * Output:
 *
 * Example 3:
 * Input: pattern = "aaaa", s = "dog cat cat dog"
 * Output: false
 *
 * Example 4:
 * Input: pattern = "abba", s = "dog dog dog dog"
 * Output: false
 */

class Solution {

    fun wordPattern(pattern: String, s: String): Boolean {
        val words = s.split(" ")
        if (pattern.length != words.size) return false

        val map = mutableMapOf<Any, Int>()

        for (i in pattern.indices) {
            val char = pattern[i]
            val word = words[i]

            if (map.put(char, i) != map.put(word, i)) return false
        }

        return true
    }

    // fun wordPattern(pattern: String, s: String): Boolean {
    //     val words = s.split(" ")
    //     if (pattern.length != words.size) return false
    //
    //     val charToWord = mutableMapOf<Char, String>()
    //     val wordToChar = mutableMapOf<String, Char>()
    //
    //     for (i in pattern.indices) {
    //         val char = pattern[i]
    //         val word = words[i]
    //
    //         if (charToWord.containsKey(char)) {
    //             if (charToWord[char] != word) return false
    //         } else {
    //             charToWord[char] = word
    //         }
    //
    //         if (wordToChar.containsKey(word)) {
    //             if (wordToChar[word] != char) return false
    //         } else {
    //             wordToChar[word] = char
    //         }
    //     }
    //
    //     return true
    // }

    // fun wordPattern(pattern: String, s: String): Boolean {
    //     val words = s.split(" ")
    //
    //     if (words.size != pattern.length) return false
    //
    //     var collection = mutableMapOf<Char, String>()
    //
    //     for (i in 0 until pattern.length) {
    //         val char = pattern[i]
    //         val word = words[i]
    //
    //         if (char in collection) {
    //             val savedWord = collection[char]
    //             if (savedWord != word) return false
    //         } else {
    //             collection[char] = word
    //         }
    //     }
    //
    //     return true
    // }
}
